/********************************************************************
 * PROJECT: CSAIR ROST PROJECT.
 * SUPPLIER: FOREVER OPEN SOURCE SOFTWARE INC.
 ********************************************************************
 * FILE: GanttMeasureComposite.java
 * (C) Copyright Foliage Team 2013, All Rights Reserved.
 ********************************************************************/
package org.foliage.rcp.ui.custom;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Spinner;
import org.foliage.rcp.ui.preference.GanttMeasure;
import org.foliage.rcp.ui.preference.IGanttSettings;

public class GanttMeasureComposite extends Composite {

	// ----------------------------------------------- 

	private GanttMeasure measure;

	private IGanttSettings settings;

	// ----------------------------------------------- 

	private Combo timeUnitCombo;

	private Combo timeFormatCombo;

	private Spinner unitCountSpinner;

	private Combo alignCombo;

	private Combo modelCombo;

	private Spinner widthSpinner;

	private Combo timeZoneCombo;

	// ----------------------------------------------- 

	private static Map<Integer, Integer> timeUnitMap = new HashMap<Integer, Integer>();
	static {
		timeUnitMap.put(0, Calendar.MINUTE);
		timeUnitMap.put(1, Calendar.HOUR_OF_DAY);
		timeUnitMap.put(2, Calendar.DAY_OF_MONTH);
	}
	private static Map<Integer, Integer> timeUnitReverseMap = new HashMap<Integer, Integer>();
	static {
		timeUnitReverseMap.put(Calendar.MINUTE, 0);
		timeUnitReverseMap.put(Calendar.HOUR_OF_DAY, 1);
		timeUnitReverseMap.put(Calendar.DAY_OF_MONTH, 2);
	}

	// ----------------------------------------------- 

	private static Map<Integer, String> timeFormatMap = new HashMap<Integer, String>();
	static {
		timeFormatMap.put(0, "MMM dd, ''HH");
		timeFormatMap.put(1, "HH:mm");
		timeFormatMap.put(2, "yyyy MMM");
	}
	private static Map<String, Integer> timeFormatReverseMap = new HashMap<String, Integer>();
	static {
		timeFormatReverseMap.put("MMM dd, ''HH", 0);
		timeFormatReverseMap.put("HH:mm", 1);
		timeFormatReverseMap.put("yyyy MMM", 2);
	}

	// ----------------------------------------------- 

	private static Map<Integer, Integer> modelMap = new HashMap<Integer, Integer>();
	static {
		modelMap.put(0, 2);
	}
	private static Map<Integer, Integer> modelReverseMap = new HashMap<Integer, Integer>();
	static {
		modelReverseMap.put(2, 0);
	}

	// ----------------------------------------------- 

	private static Map<Integer, Integer> alignMap = new HashMap<Integer, Integer>();
	static {
		alignMap.put(0, SWT.LEFT);
		alignMap.put(1, SWT.CENTER);
		alignMap.put(2, SWT.RIGHT);
	}
	private static Map<Integer, Integer> alignReverseMap = new HashMap<Integer, Integer>();
	static {
		alignReverseMap.put(SWT.LEFT, 0);
		alignReverseMap.put(SWT.CENTER, 1);
		alignReverseMap.put(SWT.RIGHT, 2);
	}

	// ----------------------------------------------- 

	private static Map<Integer, String> timeZoneMap = new HashMap<Integer, String>();
	static {
		timeZoneMap.put(0, "Etc/UTC");
		timeZoneMap.put(1, "Asia/Shanghai");
	}
	private static Map<String, Integer> timeZoneReverseMap = new HashMap<String, Integer>();
	static {
		timeZoneReverseMap.put("Etc/UTC", 0);
		timeZoneReverseMap.put("Asia/Shanghai", 1);
	}

	// ----------------------------------------------- 

	public GanttMeasureComposite(Composite parent, int style) {
		super(parent, style);
		init();
	}

	// ----------------------------------------------- 

	void init() {
		setLayout(new FillLayout(SWT.VERTICAL));
		{
			Group group = new Group(this, SWT.NONE);
			group.setText("时间格式");
			group.setLayout(new GridLayout(4, false));
			{
				Label label = new Label(group, SWT.NONE);
				label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
				label.setText("单位：");
			}
			{
				timeUnitCombo = new Combo(group, SWT.PUSH);
				timeUnitCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
				timeUnitCombo.add("分", 0);
				timeUnitCombo.add("时", 1);
				timeUnitCombo.add("日", 2);
			}
			{
				Label label = new Label(group, SWT.NONE);
				GridData data = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
				data.horizontalIndent = 10;
				label.setLayoutData(data);
				label.setText("标签：");
			}
			{
				timeFormatCombo = new Combo(group, SWT.PUSH);
				timeFormatCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
				timeFormatCombo.add("MMM dd, ''HH", 0);
				timeFormatCombo.add("HH:mm", 1);
				timeFormatCombo.add("yyyy MMM", 2);
			}
			{
				Label label = new Label(group, SWT.NONE);
				label.setText("计数：");
			}
			{
				unitCountSpinner = new Spinner(group, SWT.BORDER);
				unitCountSpinner.setSelection(1);
			}
			{
				Label label = new Label(group, SWT.NONE);
				label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
				label.setText("对齐：");
			}
			{
				alignCombo = new Combo(group, SWT.PUSH);
				GridData data = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1);
				data.widthHint = 100;
				alignCombo.setLayoutData(data);
				alignCombo.add("居左", 0);
				//				alignCombo.add("居中", 1);
				//				alignCombo.add("居右", 2);
			}
		}
		{
			Group group = new Group(this, SWT.NONE);
			group.setText("刻度模式");
			GridLayout layout = new GridLayout(4, false);
			layout.marginRight = 100;
			group.setLayout(layout);
			{
				Label label = new Label(group, SWT.NONE);
				label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
				label.setText("显示：");
			}
			{
				modelCombo = new Combo(group, SWT.PUSH);
				modelCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
				modelCombo.addListener(SWT.Show, new Listener() {

					@Override
					public void handleEvent(Event event) {
						System.out.println("TEST");
					}
					
				});
				// modelCombo.add("一层（底层）", 0);
				modelCombo.add("两层（中层、底层）", 0);
				// modelCombo.add("三层（顶层、中层、底层）", 2);
			}
			{
				Label label = new Label(group, SWT.NONE);
				GridData data = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
				data.horizontalIndent = 10;
				label.setLayoutData(data);
				label.setText("宽度：");
			}
			{
				widthSpinner = new Spinner(group, SWT.BORDER);
			}
			{
				Label label = new Label(group, SWT.NONE);
				label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
				label.setText("时区：");
			}
			{
				timeZoneCombo = new Combo(group, SWT.PUSH);
				timeZoneCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
				timeZoneCombo.add("Etc/UTC", 0);
				timeZoneCombo.add("Asia/Shanghai", 1);
			}
			new Label(group, SWT.NONE);
			new Label(group, SWT.NONE);
		}
	}

	// ----------------------------------------------- 

	public void setEnabled(boolean isEnabled) {
		timeUnitCombo.setEnabled(isEnabled);
		timeFormatCombo.setEnabled(isEnabled);
		unitCountSpinner.setEnabled(isEnabled);
		alignCombo.setEnabled(isEnabled);
	}

	public void resetGanttMeasure() {
		if (measure != null) {
			measure.setAlign(alignMap.get(alignCombo.getSelectionIndex()));
			measure.setDateFormat(timeFormatMap.get(timeFormatCombo.getSelectionIndex()));
			measure.setUnit(timeUnitMap.get(timeUnitCombo.getSelectionIndex()));
			measure.setUnitCount(unitCountSpinner.getSelection());
		}
	}

	public void resetGanttSettings() {
		settings.setGanttMeasureWidth(widthSpinner.getSelection());
		settings.setTimeZone(TimeZone.getTimeZone(timeZoneMap.get(timeZoneCombo.getSelectionIndex())));
	}

	public void resetControls(GanttMeasure ganttMeasure, IGanttSettings ganttSettings) {
		measure = ganttMeasure;
		settings = ganttSettings;
		setEnabled(measure != null);
		if (measure != null) {
			timeUnitCombo.select(timeUnitReverseMap.get(measure.getUnit()));
			timeFormatCombo.select(timeFormatReverseMap.get(measure.getDateFormat()));
			unitCountSpinner.setSelection(measure.getUnitCount());
			alignCombo.select(alignReverseMap.get(measure.getAlign()));
		}
		widthSpinner.setSelection(settings.getGanttMeasureWidth());
		timeZoneCombo.select(timeZoneReverseMap.get(settings.getTimeZone().getID()));
		modelCombo.select(modelReverseMap.get(settings.getGanttMeasureLevel()));
	}

	// ----------------------------------------------- 

	public Combo getModelCombo() {
		return modelCombo;
	}

	public void setModelCombo(Combo modelCombo) {
		this.modelCombo = modelCombo;
	}

	public Spinner getWidthSpinner() {
		return widthSpinner;
	}

	public void setWidthSpinner(Spinner widthSpinner) {
		this.widthSpinner = widthSpinner;
	}

	public Combo getTimeZoneCombo() {
		return timeZoneCombo;
	}

	public void setTimeZoneCombo(Combo timeZoneCombo) {
		this.timeZoneCombo = timeZoneCombo;
	}

}
